package com.stars.controller.advice;

import com.stars.common.base.BaseCode;
import com.stars.common.base.BaseMsg;
import com.stars.common.base.ResultMsg;
import com.stars.common.valid.MyException;
import org.jboss.logging.Logger;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.servlet.http.HttpServletRequest;

/**
 * @author alex
 * @version 1.0
 * @description 全局异常处理器, 此类需要Controller类继承才有效。否则无效
 * @time 2016年12月2日 下午2:11:36
 */
@RestControllerAdvice
public class CommonExceptionHandler {


    Logger log = Logger.getLogger(CommonExceptionHandler.class);


    /**
     * 数据校验异常处理(表单)
     */
    @ExceptionHandler(value = {BindException.class})
    public ResultMsg bindExceptionHandler(HttpServletRequest request, BindException ex) throws Exception {
        BindingResult bindingResult = ex.getBindingResult();
        return ResultMsg.customMsg(BaseCode.VLIDATE_PARM, bindingResult.getAllErrors().get(0).getDefaultMessage(), null);
    }

    /**
     * 数据校验异常处理(json)
     */
    @ExceptionHandler(value = {MethodArgumentNotValidException.class})
    public ResultMsg bindMethodArgumentNotValidHandler(HttpServletRequest request, MethodArgumentNotValidException me) throws Exception {
        BindingResult bindingResult = me.getBindingResult();
        return ResultMsg.customMsg(BaseCode.VLIDATE_PARM, bindingResult.getAllErrors().get(0).getDefaultMessage(), null);
    }

    /**
     * @param e Exception
     * @description 错误日志记录入log4j
     * @author alex
     * @return 异常错误，统一跳入错误页面
     */
    @ExceptionHandler(Exception.class)
    public ResultMsg commonException(Exception e) {
        return getResultMsg(e.toString(), e.getStackTrace(), BaseCode.SYSTEM_ERROR, BaseMsg.SYSTEM_ERROR);
    }

    /**
     * @param e NumberFormatException
     * @description 错误日志记录入log4j
     * @author alex
     * @return 异常错误，统一跳入错误页面
     */
    @ExceptionHandler(NumberFormatException.class)
    public ResultMsg commonException(NumberFormatException e) {
        return getResultMsg(e.toString(), e.getStackTrace(), BaseCode.VLIDATE_PARM, "参数异常");
    }

    /**
     * @param e MethodArgumentTypeMismatchException
     * @description 错误日志记录入log4j
     * @author alex
     * @return 异常错误，统一跳入错误页面
     */
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public ResultMsg commonException(MethodArgumentTypeMismatchException e) {
        return getResultMsg(e.toString(), e.getStackTrace(), BaseCode.VLIDATE_PARM, "参数异常");
    }

    @ExceptionHandler(value = MyException.class)
    public ResultMsg myErrorHandler(MyException ex) {
        return ResultMsg.failure(ex.getMessage());
    }
    private ResultMsg getResultMsg(String s2, StackTraceElement[] stackTrace, String vlidateParm, String 参数异常) {
        String errorMessage = s2 + "\r\n";
        StackTraceElement[] trace = stackTrace;
        for (StackTraceElement s : trace) {
            errorMessage += "\tat " + s + "\r\n";
        }
        log.error(errorMessage);
        return ResultMsg.customMsg(vlidateParm, 参数异常, null);
    }
}
